package com.baseframe.file.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baseframe.auth.utils.AuthUtil;
import com.baseframe.file.entity.BaseFile;
import com.baseframe.file.entity.BaseOss;
import com.baseframe.file.entity.requset.BaseFileRequest;
import com.baseframe.file.entity.vo.BaseFileVO;
import com.baseframe.file.service.BaseFileService;
import com.baseframe.file.mapper.BaseFileMapper;
import com.baseframe.file.service.BaseOssService;
import com.baseframe.log.exception.ServiceException;
import com.baseframe.mybatis.utils.PageInfo;
import com.baseframe.mybatis.utils.PageUtil;
import com.baseframe.oss.builds.BaseOssBuilds;
import com.baseframe.oss.entity.Oss;
import com.baseframe.oss.entity.OssFile;
import com.baseframe.oss.sign.OssType;
import com.baseframe.oss.template.OssTemplate;
import com.baseframe.tool.result.R;
import com.baseframe.tool.utils.data.StringUtil;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**

* @description 针对表【base_file】的数据库操作Service实现
* @createDate 2023-06-28 06:42:04
*/
@Service
@AllArgsConstructor
public class BaseFileServiceImpl extends ServiceImpl<BaseFileMapper, BaseFile>
    implements BaseFileService{

    private BaseOssService baseOssService;

    @Override
    public R<Object> uploadFile(MultipartFile file) {
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //todo 后续考虑存储oss到redis中

        BaseOss baseOss = baseOssService.getOne(new LambdaQueryWrapper<BaseOss>().eq(BaseOss::getStatus, 1));
        if (baseOss==null){
            return R.fail("文件上传失败，无启用的oss配置~");
        }
        Oss oss = new Oss();
        BeanUtils.copyProperties(baseOss,oss,"id","type");
        oss.setId(String.valueOf(baseOss.getId()));
        oss.setType(OssType.getOssType(baseOss.getType()));
        OssTemplate ossTemplate = BaseOssBuilds.getTemplate(oss);
        OssFile ossFile = ossTemplate.putFile(file.getOriginalFilename(),inputStream);
        if (ossFile!=null){
            BaseFile baseFile = new BaseFile();
            baseFile.setFilename(ossFile.getFilename());
            baseFile.setFileKey(ossFile.getKey());
            baseFile.setOriginalFilename(ossFile.getOriginalFilename());
            baseFile.setLink(ossFile.getLink());
            baseFile.setUserId(AuthUtil.getUserId());
            save(baseFile);
        }
        return R.data(ossFile);


    }

    @Override
    public BaseFile getFileInfo(String fileUrl) {

        return getOne(new LambdaQueryWrapper<BaseFile>().eq(BaseFile::getLink,fileUrl));
    }

    @Override
    public Page<BaseFileVO> getFilePageList(PageInfo pageInfo) {
        Page<BaseFileVO> page = PageUtil.buildPage(pageInfo, BaseFileVO.class);
        Page<BaseFileVO> filePage = getBaseMapper().getFilePageList(page, AuthUtil.getUserId());
        for (BaseFileVO record : filePage.getRecords()) {
            String label = record.getLabel();
            if(StringUtil.isNotBlank(label)){
                String[] split = label.split(",");
                List<String> labelList = Arrays.asList(split);
                record.setLabelList(labelList);
            }
        }
        return filePage;
    }

    @Override
    public boolean addLabel(BaseFileRequest request) {
        BaseFile baseFile = getById(request.getId());
        if(baseFile == null){
            throw new ServiceException(400,"文件资源不存在");
        }

        if( request.getLabelList().size()>=5){
            throw new ServiceException(400,"文件标签不能超过五个");
        }
        String join = String.join(",", request.getLabelList());

        return update(new LambdaUpdateWrapper<BaseFile>().eq(BaseFile::getId,request.getId()).set(BaseFile::getLabel,join));
    }

    @Override
    public boolean deleteLabel(BaseFileRequest request) {
        BaseFile baseFile = getById(request.getId());
        if(baseFile == null){
            throw new ServiceException(400,"文件资源不存在");
        }
        String label = String.join(",", request.getLabelList());
        return update(new LambdaUpdateWrapper<BaseFile>().eq(BaseFile::getId,request.getId()).set(BaseFile::getLabel,label));
    }
}




